# coding: utf-8
import io

from PySide6.QtGui import QImage, QPixmap
from qfluentwidgets import isDarkTheme
from qrcode import QRCode


class QRCodeHelper:
    @classmethod
    def generate_qr_code(cls, data: str) -> bytes:
        qr = QRCode()
        qr.add_data(data)
        qr.make(fit=True)

        # 创建一个带有透明背景的图像
        color = 'white' if isDarkTheme() else 'black'
        img = qr.make_image(fill_color='black', back_color="white")
        buffer = io.BytesIO()
        img.save(buffer, format='PNG')
        return buffer.getvalue()

    @classmethod
    def generate_qr_code_image(cls, data: str) -> QImage:
        image_bytes = cls.generate_qr_code(data)
        return QImage.fromData(image_bytes)

    @classmethod
    def generate_qr_code_pixmap(cls, data: str) -> QPixmap:
        image = cls.generate_qr_code_image(data)
        return QPixmap.fromImage(image)

if __name__ == '__main__':
    url = 'https://t.cn/RtcdjqX?code=fab4dd0103f94586808555b2157a0335'
    QRCodeHelper.generate_qr_code_image(url).save('qrcode.png')